package com.atguigu.guli.service.base.exception;


import com.atguigu.guli.service.base.result.R;

import com.atguigu.guli.service.base.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.sql.ResultSetMetaData;

@RestControllerAdvice
@Slf4j
public class GolableExceotionHandler {

    @ExceptionHandler(value = Exception.class)
    public R exception(Exception e){
        log.error("异常+" + ExceptionUtils.getStackTrace(e));
        return R.error().message("未知异常");
    }

    @ExceptionHandler(value = BadSqlGrammarException.class)
    public R badSqlGrammarException(BadSqlGrammarException e){
        log.error("异常+" + ExceptionUtils.getStackTrace(e));
        return R.error().message(ResultCodeEnum.BAD_SQL_GRAMMAR.getMessage());
    }

    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
    public R methodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e){
        log.error("异常"+ExceptionUtils.getStackTrace(e));
        return R.error().message(ResultCodeEnum.JSON_PARSE_ERROR.getMessage());
    }

    @ExceptionHandler(value = GuliException.class)
    public R methodGuliException(GuliException e){
        log.error("异常"+ExceptionUtils.getStackTrace(e));
        return R.error().message(e.getMessage()).code(e.getCode());
    }
}
